package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CommentsMapper;
import com.ruoyi.system.domain.Comments;
import com.ruoyi.system.service.ICommentsService;
import com.ruoyi.system.es.CommentDocument;
import com.ruoyi.system.es.CommentRepository;

/**
 * 评论管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-25
 */
@Service
public class CommentsServiceImpl implements ICommentsService 
{
    @Autowired
    private CommentsMapper commentsMapper;

    @Autowired
    private CommentRepository commentRepository;

    /**
     * 查询评论
     * 
     * @param commentId 评论主键
     * @return 评论
     */
    @Override
    public Comments selectCommentsByCommentId(Long commentId)
    {
        return commentsMapper.selectCommentsByCommentId(commentId);
    }

    /**
     * 查询评论列表
     * 
     * @param comments 评论
     * @return 评论
     */
    @Override
    public List<Comments> selectCommentsList(Comments comments)
    {
        return commentsMapper.selectCommentsList(comments);
    }

    /**
     * 新增评论
     * 
     * @param comments 评论
     * @return 结果
     */
    @Override
    public int insertComments(Comments comments)
    {
        int result = commentsMapper.insertComments(comments);
        if (result > 0) {
            CommentDocument doc = new CommentDocument();
            doc.setSId(comments.getCommentId().toString());
            doc.setId(comments.getCommentId()+":comments");
            doc.setPostId(comments.getPostId());
            doc.setContent(comments.getContent());
            doc.setAuthor(comments.getUserName());
            doc.setCreateTime(comments.getCreateTime());
            commentRepository.save(doc);
        }
        return result;
    }

    /**
     * 修改评论
     * 
     * @param comments 评论
     * @return 结果
     */
    @Override
    public int updateComments(Comments comments)
    {
        int result = commentsMapper.updateComments(comments);
/*        if (result > 0) {
            CommentDocument doc = new CommentDocument();
            doc.setId(comments.getCommentId()+":comments");
            doc.setPostId(comments.getPostId());
            doc.setContent(comments.getContent());
            doc.setAuthor(comments.getUserName());
            doc.setCreateTime(comments.getCreateTime());
            commentRepository.save(doc);
        }*/
        return result;
    }

    /**
     * 批量删除评论
     * 
     * @param commentIds 需要删除的评论主键
     * @return 结果
     */
    @Override
    public int deleteCommentsByCommentIds(Long[] commentIds)
    {
        return commentsMapper.deleteCommentsByCommentIds(commentIds);
    }

    /**
     * 删除评论信息
     * 
     * @param commentId 评论主键
     * @return 结果
     */
    @Override
    public int deleteCommentsByCommentId(Long commentId)
    {
        int result = commentsMapper.deleteCommentsByCommentId(commentId);
        if (result > 0) {
            commentRepository.deleteById(commentId+":comments");
        }
        return result;
    }

    /**
     * 审核通过评论
     * 
     * @param comments 评论信息
     * @return 结果
     */
    @Override
    public int approveComments(Comments comments)
    {
        // 批量更新评论状态为正常(1)
        if (comments.getCommentIds() != null && comments.getCommentIds().length > 0) {
            return commentsMapper.updateCommentsStatus(comments.getCommentIds(), 1);
        }
        return 0;
    }

    /**
     * 屏蔽评论
     * 
     * @param comments 评论信息
     * @return 结果
     */
    @Override
    public int rejectComments(Comments comments)
    {
        // 批量更新评论状态为被屏蔽(2)
        if (comments.getCommentIds() != null && comments.getCommentIds().length > 0) {
            return commentsMapper.updateCommentsStatus(comments.getCommentIds(), 2);
        }
        return 0;
    }
}
